<?php

    ##################################################
    #
    # Copyright (c) 2008 - 2010 ABSoft Company
    # Written and Designed by Dang Tin Trung
    #
    # This file is part of ABSoft Framework
    #
    ##################################################

    /**
    * @author: immanuel192
    * @since: Oct 28, 2010
    * 
    * This is ajax control in form
    * This control help user to select images will be shown in news
    * 
    * - Create news first
    * - End then, edit your news with this control
    * - And save your news
    * 
    * Update logs
    * Oct 28, 2010 - immanuel192 - first create
    */
    if (!defined('EXPONENT')) exit('');

    /**
    * Ajax Select File Control
    *
    * @author Dang Tin Trung
    * @copyright 2008 - 2009 ABSoft Company
    * @version 1.0
    * 
    * @package Subsystems
    * @subpackage Forms
    */

    /**
    * Manually include the class file for formcontrol, for PHP4
    * (This does not adversely affect PHP5)
    */
    require_once(BASE."subsystems/forms/controls/formcontrol.php");

    /**
    * Ajax Select File Control
    *
    * @package Subsystems
    * @subpackage Forms
    */
    class ajax_image_list extends formcontrol {
        public $width=100;
        public $height=150;
        function name() { return "Ajax Image List Control"; }
        function isSimpleControl() { return true; }
        function getFieldDefinition() {
            return array(
            DB_FIELD_TYPE=>DB_DEF_STRING,
            DB_FIELD_LEN=>250,
            );
        }
        
        /**
        * Ajax Image List - Images Listbox for news
        * 
        * @param string  ID of your image listbox
        * @param int Id of image collection
        * @param int  News ID
        * @param bool  Load JS or not?
        */
        function ajax_image_list($id_name = "imagelist", $collection_id = 0, $news_id=-1, $load_js = false) {
            $this->load_js = $load_js;
            $this->id_name = $id_name;
            $this->collection_id = $collection_id;
            $this->news_id = $news_id;
        }

        function onRegister(&$form) {
            // we dont need this enctype
            //$form->enctype = "multipart/form-data";
        }

        function controlToHTML($name) {
            $html='';
            global $db;
            // get file name
            if ($this->news_id != -1)
            {
                $temp=$db->selectObject('newsitem_image','news_id = '.$this->news_id);
                if ($temp)
                {
                    // load all images of this news into listbox
                }
            }
            if ($this->load_js)
            {
                // load init js
            }
            $html.="
            <select id='{$this->id_name}' name='{$this->id_name}' style='width:{self::width}; height:{self::$height};'>
            </select>
            ";
            return $html;
        }

        function form($object) {
            if (!defined("SYS_FORMS")) require_once(BASE."subsystems/forms.php");
            exponent_forms_initialize();

            $form = new form();
            if (!isset($object->identifier)) {
                $object->identifier = "";
                $object->caption = "";
                $object->default = "";
            }
            $i18n = exponent_lang_loadFile('subsystems/forms/controls/textcontrol.php');

            $form->register("identifier",$i18n['identifier'],new textcontrol($object->identifier));
            $form->register("caption",$i18n['caption'], new textcontrol($object->caption));
            $form->register("default",$i18n['default'], new textcontrol($object->default));
            $form->register("submit","",new buttongroupcontrol($i18n['save'],'',$i18n['cancel']));
            return $form;
        }

        function update($values, $object) {
            if ($object == null) $object = new uploadcontrol();
            if ($values['identifier'] == "") {
                $i18n = exponent_lang_loadFile('subsystems/forms/controls/textcontrol.php');
                $post = $_POST;
                $post['_formError'] = $i18n['id_req'];
                exponent_sessions_set("last_POST",$post);
                return null;
            }
            $object->identifier = $values['identifier'];
            $object->caption = $values['caption'];
            $object->default = $values['default'];
            return $object;
        }

        function parseData($original_name,$formvalues) {
            $file = $formvalues[$original_name];
            return '<a href="'.URL_FULL.$file.'">'.basename($file).'</a>';
        }
    }

?>
